<template>
    <div class="detail">
        <div class="title">
            <div class="left">
                <h1>{{ store.articleDetail?.title }}</h1>
                <div class="stats">
                    <div class="stat-item">
                        <img src="../images/lll.svg" alt="views">
                        <span>{{ store.articleDetail?.views }}</span>
                    </div>
                    <div class="stat-item good-item" @click.once="good">
                        <img :src="isGood ? good2 : goodImg" alt="likes">
                        <span>{{ store.articleDetail?.good_count }}</span>
                    </div>
                </div>
            </div>
            <el-button @click="router.push('/')">返回</el-button>
        </div>
        <div id="markdown"></div>
    </div>
</template>

<script setup lang="ts">
import { nextTick, onMounted, ref } from 'vue'
import useStore from '../store.ts'
import { useRoute, useRouter } from 'vue-router'
import Vditor from "vditor";
import "vditor/dist/index.css";
import good2 from '../images/good2.svg'
import goodImg from '../images/good.svg'

const store = useStore()
const router = useRouter()
const route = useRoute()
const vditor = ref<Vditor>();
const isGood = ref(false)


onMounted(() => {

    initVditor();
    // 然后获取数据
    store.getArticleDetailData(route.params.id)

})


const initVditor = () => {
    // 初始化富文本编辑器
    vditor.value = new Vditor("markdown", {
        after() {
            vditor.value!.setValue(store.articleDetail!.content);
            vditor.value?.disabled()
        },
        height: 400,
        value: "loading...",
        toolbar: [],
        toolbarConfig: {
            hide: false,
            pin: false,
        },
        outline: {
            enable: true,
            position: "right",
        },
        cache: {
            enable: false,
            after() {
            },
        },
    })
}

const good = () => {
    isGood.value = true
    let newGood = store.articleDetail!.good_count + 1
    store.addGoodData(route.params.id, newGood)
        .then(() => {
            store.getArticleDetailData(route.params.id)
        })

}



</script>

<style scoped>
.detail {
    height: 100%;
    width: 1260px;
    padding: 30px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    position: relative;
    overflow: hidden;

    &::before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 4px;
    }

    .title {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 25px;
        padding-bottom: 15px;
        border-bottom: 1px solid rgba(0, 0, 0, 0.08);

        .left {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;

            h1 {
                margin: 0;
                font-size: 28px;
                color: #1f2d3d;
                font-weight: 700;
                line-height: 1.3;
                max-width: 900px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                text-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
            }

            .stats {
                display: flex;
                align-items: center;
                gap: 20px;
                background: rgba(245, 247, 250, 0.8);
                padding: 8px 15px;
                border-radius: 50px;
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

                .stat-item {
                    display: flex;
                    align-items: center;
                    gap: 6px;
                    font-size: 15px;
                    color: #4a5568;

                    img {
                        height: 20px;
                        width: 20px;
                        opacity: 0.85;
                        transition: all 0.2s;
                    }

                    &.good-item {
                        cursor: pointer;

                        &:hover {
                            img {
                                transform: scale(1.15);
                                opacity: 1;
                            }
                        }
                    }
                }
            }
        }

        .el-button {
            padding: 10px 24px;
            background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
            border: none;
            color: #4a5568;
            font-weight: 500;
            border-radius: 8px;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

            &:hover {
                background: linear-gradient(135deg, #409eff 0%, #42b983 100%);
                color: white;
                transform: translateY(-2px);
                box-shadow: 0 6px 16px rgba(64, 158, 255, 0.3);
            }
        }
    }

    #markdown {
        padding: 0;
        background: transparent;
        min-height: 600px;
        width: 100%;

        /* Vditor 深度样式调整 */
        :deep(.vditor) {
            background: transparent;

            .vditor-content {
                padding: 0 10px;
                background: transparent;
            }

            .vditor-reset {
                padding: 0 15px !important;
                font-size: 16px;
                line-height: 1.8;
                color: #3a3a3a;

                h1,
                h2,
                h3,
                h4,
                h5,
                h6 {
                    color: #1f2d3d;
                    margin-top: 1.5em;
                    margin-bottom: 0.8em;
                    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
                    padding-bottom: 0.3em;
                }

                a {
                    color: #409eff;
                    text-decoration: none;
                    border-bottom: 1px dashed rgba(64, 158, 255, 0.4);

                    &:hover {
                        color: #42b983;
                        border-bottom-style: solid;
                    }
                }

                code {
                    background: rgba(66, 185, 131, 0.1);
                    color: #42b983;
                    padding: 0.2em 0.4em;
                    border-radius: 4px;
                    font-size: 0.9em;
                }

                pre {
                    background: rgba(245, 247, 250, 0.8);
                    border-radius: 8px;
                    padding: 1em;
                    border-left: 4px solid #409eff;
                }

                blockquote {
                    border-left: 4px solid #42b983;
                    background: rgba(66, 185, 131, 0.05);
                    padding: 0.5em 1em;
                    color: #4a5568;
                    border-radius: 0 8px 8px 0;
                }
            }

            .vditor-outline {
                background: rgba(255, 255, 255, 0.9);
                border-radius: 8px;
                padding: 10px;
                box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
                right: 5px !important;
                width: 220px !important;

                li {
                    padding: 4px 0;

                    a {
                        color: #4a5568;

                        &:hover {
                            color: #409eff;
                        }
                    }
                }
            }
        }
    }
}

/* 响应式设计 */
@media (max-width: 992px) {
    .detail {
        width: 100%;
        padding: 20px;

        .title {
            flex-direction: column;
            align-items: flex-start;
            gap: 15px;

            .left {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;

                h1 {
                    max-width: 100%;
                    font-size: 24px;
                }

                .stats {
                    width: 100%;
                    justify-content: space-around;
                }
            }

            .el-button {
                width: 100%;
                margin-top: 10px;
            }
        }

        #markdown {
            :deep(.vditor-outline) {
                display: none;
            }
        }
    }
}
</style>